<?php

/**
 * @author HuyHoang
 * @copyright 2011
 */
ob_start();
class FrontendsController extends AppController {
    var $name = 'Frontends';
    var $helpers = array('Html','Ajax','Javascript','Form');
	
    var $paginate = array(
        'limit' => 10
    );
	var $uses = array('Frontend','Product','Provider','Shop','ParametersProduct','Parameter','Comment','Offsale','Customer','Paymethod','Order','Orderdetail');
    function index($id = null) {
        $this->layout = "frontend";
        $this->set('productview',$this->Product->find('all'));
    }
    
    function save() {
        $this->redirect(array("action" => "index", $this->Customer->id));
    }
    
    function delete($id = null) {
        $this->redirect(array("action" => "index"));
    }
	function view($id = null){
	$this->layout = "frontend";
	$this->Product->id = $id;
	$this->set('vProduct',$this->Product->read());
	$this->set('pro',$this->Product->find('all'));
	$this->set('providerList', $this->Provider->find('all'));
	$this->set('shop',$this->Shop->find('all'));
	$this->set('parametersp',$this->ParametersProduct->find('all'));
	$this->set('paramsp',$this->Parameter->find('all'));
	$this->set('comments',$this->Comment->find('all'));
	$this->set('offsales',$this->Offsale->find('all'));
	$this->set('customer',$this->Customer->find('all'));
	
	}
function addtocart($id=NULL) {
	$this->autoRender = false;
	if(!$this->Session->check("cart")) $this->Session->write("cart"); //neu chua co bien cart thi khoi tao bien cart
    if (count($this->Session->read("cart")) > 0) {
        if (array_key_exists($id, $this->Session->read("cart"))) { //kiem tra xem product_id da ton tai chua
            $soluong = $this->_checkKhoHang($id); //check so luong trong kho hang theo id san pham
            if ($_SESSION["cart"][$id] < $soluong) { //neu so luong trong gio hang < so luong trong kho thi cho add
			 $_SESSION["cart"][$id]++;
            } else {
               echo "<script>alert('Xin loi kho hang chung toi khong du!')</script>";
            }
        } else {
            $this->Session->write("cart.{$id}",1);
        }
    }else{
       $this->Session->write("cart.{$id}",1);
    }
	$this->Session->write("tongsanpham",$this->_layTongSanPhamTrongGioHang($this->Session->read("cart")));
	$this->Session->write("tongtien",$this->_layTongGiaSanPhamTrongGioHang($this->Session->read("cart")));
	print_r($this->Session->read("cart"));
	
}

	function _layTongSanPhamTrongGioHang($array_cart) {
		$total = 0;
		if(is_array($array_cart)){
			foreach ($array_cart as $id => $qty) {
				$total+=$qty;
			}
		}
    return $total;
	}

	function _layTongGiaSanPhamTrongGioHang($array_cart) {
		$total = 0;
		if(is_array($array_cart)){
			foreach ($array_cart as $id => $qty) {
				$price = $this->_layGiaTungSanPham($id);
				$total+=$price * $qty;
			}
		}
		return $total;
	}

	function _layGiaTungSanPham($id) {
		$priceProduct = $this->Product->read(NULL,$id);
		$price = $priceProduct['Product']['price'];
		return $price;
	}

	function _checkKhoHang($id) {
		$slproduct = $this->Product->read(NULL,$id);
		$sl = $slproduct['Product']['quantity'];
		return $sl;
	}
	function _layThongTinSanPham($id){
		$this->loadModel("Product");
		$wproduct = $this->Product->read(NULL,$id);
		return $wproduct;
	}
	function updatecart() {
 $this->autoRender=false;
    foreach ($this->Session->read("cart") as $id => $qty) {
        if ($_POST[$id] <= 0) {
		unset($_SESSION["cart"][$id]);
        } else {
            $soluong = $this->_checkKhoHang($id);
            if ($_POST[$id] < $soluong) {
			$_SESSION["cart"][$id] = $_POST[$id];
            }else {
                echo "<script>alert('Xin loi kho hang chung toi khong du!')</script>";
            }
        }
    }
	$this->Session->write("tongsanpham",$this->_layTongSanPhamTrongGioHang($this->Session->read("cart")));
	$this->Session->write("tongtien",$this->_layTongGiaSanPhamTrongGioHang($this->Session->read("cart")));
	$this->redirect(array('action'=>'viewcart'));
}

function deleteItemCart($id) {
	$this->autoRender= false;
	unset($_SESSION["cart"][$id]);
	$this->redirect(array('action'=>'viewcart'));
}

function deleteCart() {
	$this->autoRender= false;
	unset($_SESSION["cart"]);
	$this->redirect(array('action'=>'index'));
}
function viewcart($id=null){
	$this->layout = "frontend";	
	$this->Product->id = $id;
	if($this->Session->read("cart")){
	$this->set('viewcarts',$this->Product->find('all'));
	$this->set("ListPayMethod", $this->Paymethod->find("all"));
	}
}


// end cart
}

?>
